// packageA/pages/equipmentMaintainList/equipmentMaintainList.js
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参
    paramObj:{},
    //列表
    list:[],
    //无数据
    showNothing:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('保养传参');
      console.log(paramObj);

      this.setData({
        paramObj: paramObj
      })

      if(paramObj.repertoryId){
        //获取保养列表
        this.getList();
      }
    }
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });  
    
    let param = {
      isAuth: false,
      className: 'maintain',
      data: {
        repertoryId: this.data.paramObj.repertoryId,
        searchAll: true,
        selectFields: ['id','startDate','endDate','maintainer','parts','remarks'],
        sort: {id: -1} 
      }
    }

    CommonService.goFindList(param).then(res => {
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('保养列表');
        console.log(result);

        this.setData({
          list: result
        })
      }else{    
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },
  
  //配件
  goToPart(e){
    let detail=e.currentTarget.dataset.detail;
    let obj={
      id: detail.id,
      repertoryId: this.data.paramObj.repertoryId
    }
    wx.navigateTo({
      url: "/packageA/pages/equipmentMaintainAdd/equipmentMaintainAdd?paramObj="+JSON.stringify(obj)
    })
  },

  //查看配件
  goToLookPart(e){
    let detail=e.currentTarget.dataset.detail;
    let obj={
      id: detail.id,
      repertoryId: this.data.paramObj.repertoryId,
      readOnly: true
    }
    wx.navigateTo({
      url: "/packageA/pages/equipmentMaintainAdd/equipmentMaintainAdd?paramObj="+JSON.stringify(obj)
    })
  },

  //添加保养
  goToAdd(){
    let obj={
      repertoryId: this.data.paramObj.repertoryId
    }
    wx.navigateTo({
      url: "/packageA/pages/equipmentMaintainAdd/equipmentMaintainAdd?paramObj="+JSON.stringify(obj)
    })
  },

   //刷新列表和页码
   refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})